package com.woniuxy.myfinance.controller;

import com.woniuxy.commons.annotation.RequirePerms;
import com.woniuxy.commons.entity.Finance;
import com.woniuxy.commons.entity.PageBean;
import com.woniuxy.commons.entity.UserDetails;
import com.woniuxy.commons.entity.Userinfo;
import com.woniuxy.commons.stafflog.LogRequirePerms;
import com.woniuxy.commons.utils.ResponseResult;
import com.woniuxy.myfinance.service.FinanceService;
import com.woniuxy.myfinance.service.UserdetailsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author author
 * @create 2021-11-16 12:09
 */
@RestController
@RequestMapping("/finance")
public class FinanceController {
    @Resource
    private FinanceService financeService;

    @LogRequirePerms(module = "财务模块",description = "查询财务状况")
    @RequirePerms("user:select")
    @GetMapping("/getallFinance")
    public ResponseResult<PageBean<Finance>> getallFinance(Finance finance, int pageIndex, int pageSize){
        List<Finance> finances = financeService.selectAllFinance(finance, pageIndex, pageSize);
        int i = financeService.selectFinanceCountBylimit(finance);
        PageBean pageBean=new PageBean();
        pageBean.setTotalRecord(i);
        pageBean.setPageSize(pageSize);
        pageBean.setPageIndex(pageIndex);
        pageBean.setBeanList(finances);

        ResponseResult responseResult=new ResponseResult();
        responseResult.setCode(200);
        responseResult.setMessage("查询成功");
        responseResult.setData(pageBean);
        return  responseResult;

    }


    @LogRequirePerms(module = "财务模块",description = "添加财务状况")
     @RequirePerms("user:add")
    @PostMapping("/addFinance")
    public ResponseResult addFinance(@RequestBody Finance finance){
        financeService.addFinance(finance);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setMessage("添加成功");
        responseResult.setCode(200);
        return responseResult;
    }


    @LogRequirePerms(module = "财务模块",description = "修改财务状况")
    @RequirePerms("user:update")
    @PutMapping("/updateFinance")
    public ResponseResult updateFinance(@RequestBody Finance finance ){
        financeService.updateFinance(finance);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setMessage("修改成功");
        responseResult.setCode(200);
        return responseResult;


    }



    @LogRequirePerms(module = "财务模块",description = "删除财务状况")
    @RequirePerms("user:del")
   @DeleteMapping("/delFinance/{financeid}")
    public ResponseResult delFinance(@PathVariable("financeid")int financeid){
    financeService.delectFinanceByid(financeid);
       ResponseResult responseResult=new ResponseResult();
       responseResult.setMessage("删除成功");
       responseResult.setCode(200);
       return responseResult;

    }


    @Resource
    private UserdetailsService userdetailsService;

    @PostMapping("/addUserdetails")
    @RequirePerms("user:add")
    @LogRequirePerms(module = "用户模块",description = "添加用户详情")
    public ResponseResult addUserdetails(@RequestBody UserDetails userDetails){
        System.out.println("调用方法");
        String uid = UUID.randomUUID().toString();
        userDetails.setUid(uid);
        Date currentTime = new Date();
       SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
       String dateString = formatter.format(currentTime);
       userDetails.setUdata(dateString);
       userDetails.setUimage("15656");
        userdetailsService.addUserdetails(userDetails);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setMessage("添加成功");
        responseResult.setCode(200);
        return responseResult;
    }



    @GetMapping("/getUserdetails")
    @LogRequirePerms(module = "用户模块",description = "查询用户详情")
    public ResponseResult getUserdetails(){
        List<UserDetails> allUserdetails = userdetailsService.getAllUserdetails();
        ResponseResult responseResult=new ResponseResult();
        responseResult.setMessage("查询成功");
        responseResult.setCode(200);
        responseResult.setData(allUserdetails);
        return responseResult;


    }

//    @RequirePerms("user:select")
//    @LogRequirePerms(module = "用户模块",description = "查询用户借/贷款详情")
//    @GetMapping("/getUserBylon/{lng}")
//     public ResponseResult getUserBylon(@PathVariable("lng") String lng){
//        System.out.println(lng);
//        List<Userinfo> userBylon = userdetailsService.getUserBylon(lng);
//        ResponseResult responseResult=new ResponseResult();
//        responseResult.setMessage("查询成功");
//        responseResult.setCode(200);
//        responseResult.setData(userBylon);
//        return responseResult;
//    }


    @GetMapping("/getUserBylon")
    @LogRequirePerms(module = "用户模块",description = "查询用户详情")
    public ResponseResult getUserBylon(String lng){
        System.out.println(lng);
        List<Userinfo> userBylon = userdetailsService.getUserBylon(lng);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setMessage("查询成功");
        responseResult.setCode(200);
        responseResult.setData(userBylon);
        return responseResult;

    }






}
